#include <iostream>
#include <string>
#include <ctype.h>

#include "RestockMenu.h"

using std::string;
using std::cout;
using std::endl;
using std::cin;
using std::getline;

//Constructors

RestockMenu::RestockMenu(int itemType): InteractiveMenu("Restock an item", "(You can enter -1 to return to the previous menu)")
{
	itemTypeID = itemType;
}

//Member Functions

void RestockMenu::performAction(Stock* companyStock)
{
	string itemID, quantityStr;
	int quantity;

	do {
		itemID = getItemIDFromUser();

		if (itemID.compare("-1") != 0)
		{
			int thisItemType = companyStock -> setBufferItem(itemID);

			if (thisItemType == -1)
				cout << "There is no item with ID " + itemID + "." << endl;
			else if (itemTypeID != 5 && thisItemType != itemTypeID)
				cout << "Item with ID " + itemID + " is not a " + companyStock -> getProductName(itemTypeID) + "." << endl;
			else
			{
				do {
					cout << "Please enter the quantity: ";
					getline(cin, quantityStr);

					if (quantityStr.compare("-1") != 0)
					{
						quantity = atoi(quantityStr.c_str());

						if (quantity <= 0)
							cout << "Invalid Input! ";
						else
						{
							companyStock -> addItem(itemID, quantity);
							cout << "Item with ID " + itemID + " is successfully restocked." << endl;
							quantityStr = "-1";
						}
					}
				} while (quantityStr.compare("-1") != 0);

				itemID = "-1";
			}
		}
	} while (itemID.compare("-1") != 0);

	cout << endl;
}